package com.csizg.facerecognition;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.csizg.facemodel.util.CompareResult;
import com.csizg.facemodel.util.FaceUtil;
import com.csizg.facemodel.view.ICompareListener;
import com.csizg.facemodel.view.IFaceShowListener;
import com.csizg.facemodel.view.MyVCameraView;
import com.csizg.facemodel.view.RectBitmap;
import org.opencv.android.CameraBridgeViewBase;

public class FdVActivity extends Activity {

    private static final String TAG = "OCVSample::Activity";

    public static final int JAVA_DETECTOR = 0;
    public static final int NATIVE_DETECTOR = 1;

    private String[] mDetectorName;

    private MyVCameraView mOpenCvCameraView;
    private RectBitmap mImageView;
    private ImageView mImageView1;
    private ImageView mImageView2;

    private TextView mSwitchTextView;
    private TextView mSaveTextView;
    private TextView mCompareTextView;

    public FdVActivity() {
        mDetectorName = new String[2];
        mDetectorName[JAVA_DETECTOR] = "Java";
        mDetectorName[NATIVE_DETECTOR] = "Native (tracking)";
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.fdv_layout);

        mOpenCvCameraView = findViewById(R.id.fd1_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
        mOpenCvCameraView.setWindowManager(getWindowManager());

        mImageView = findViewById(R.id.fd1_image_view);
        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        mImageView1 = findViewById(R.id.fd1_switch_image1);
        mImageView2 = findViewById(R.id.fd1_switch_image2);

        mSwitchTextView = findViewById(R.id.fd1_switch);
        mSwitchTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.switchCamera();
            }
        });

        mSaveTextView = findViewById(R.id.fd1_save);
        mSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOpenCvCameraView.setCaptureFace();
            }
        });

        mCompareTextView = findViewById(R.id.fd1_compare);
        mCompareTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCompareTextView.setText("识别中，请勿重复点击");
                mOpenCvCameraView.setCompareFace();
            }
        });

        mOpenCvCameraView.setICompareListener(new ICompareListener() {
            @Override
            public void onCompare(CompareResult result) {
                if (result == null) {
                    mCompareTextView.setText("不是同一人");
                } else {
                    mCompareTextView.setText("相似度" + result.getNum() + "%");
                    mImageView2.setImageBitmap(result.getBitmap());
                }
            }

            @Override
            public void onCapture(boolean isTrue) {
                if (isTrue) {
                    Bitmap bitmap = FaceUtil.fileToImage(mOpenCvCameraView.getCurrentPath());
                    if (bitmap != null) {
                        mImageView1.setImageBitmap(bitmap);
                    }
                }
                Toast.makeText(FdVActivity.this, isTrue ? "存储图片成功" : "存储图片失败", Toast.LENGTH_SHORT)
                        .show();
            }
        });

        mOpenCvCameraView.setIFaceShowListener(new IFaceShowListener() {
            @Override
            public void onShow(Bitmap result) {
                mImageView.updateRect(result);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    private void init() {
        System.loadLibrary("opencv_java3");
        System.loadLibrary("detection_based_tracker_jni");

        mOpenCvCameraView.enableView();
        mOpenCvCameraView.enableFpsMeter();

        Bitmap bitmap = FaceUtil.fileToImage(mOpenCvCameraView.getCurrentPath());
        if (bitmap != null) {
            mImageView1.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
        }
    }
}
